﻿using System;
using System.Data;

namespace DAL
{
    public static class DalReceipt
    {
        private static readonly clsConnect Cls = new clsConnect();
        #region[Get-Data]
        public static DataTable GetReceipt(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_Receipt_Get");
        }
        public static DataTable GetReceiptReport(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_ReceiptReport_Get");
        }
        public static DataTable GetReceiptEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID", id);
            return Cls.GetData("sp_Receipt_Get_Edit");
        }
        public static DataTable GetReceiptTotal(int id, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Sell_Total");
        }
        public static DataTable GetReceiptIsOrderTotal(int id, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Sell_Total_IsOrder");
        }
        public static bool GetRceiptCheck(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@Flag", obj.Flag);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Receipt_Check", "@Msg");
            return true;
        }
        public static bool GetRceiptCheckIsOrder(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@Flag", obj.Flag);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Receipt_CheckIsOrder", "@Msg");
            return true;
        }
        public static bool GetRceiptCheckAmout(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@AmountOutput", obj.Amount);
            Cls.AddParameter("@VoucherAmount", obj.VoucherAmount);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Receipt_Check_Amount", "@Msg");
            return true;
        }
        public static DataTable GetReceiptTotalRemain(int idSell, float voucherAmount)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", idSell);
            Cls.AddParameter("@VoucherAmount", voucherAmount);
            return Cls.GetData("sp_Receipt");
        }
        public static DataTable GetReceiptFillterDate(string dateBegin, string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_Receipt_FillterDate");
        }
        public static DataTable GetReceiptReportFillterDate(string dateBegin, string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_Receipt_Report_FillterDate");
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool InsertReceipt(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ReceiptCode", obj.ReceiptCode);
            Cls.AddParameter("@ReceiptDate", obj.ReceiptDate);
            Cls.AddParameter("@IDReceiptReason", obj.IDReceiptReason);
            Cls.AddParameter("@PayerRecipientCode", obj.PayerRecipientCode);
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@VoucherFile", obj.VoucherFile);
            Cls.AddParameter("@VoucherAmount", obj.VoucherAmount);
            Cls.AddParameter("@Amount", obj.Amount);
            Cls.AddParameter("@Amount_Remain", obj.Amount_Remain);
            Cls.AddParameter("@Note", obj.Note);
            return Cls.ExecuteNonQuery("sp_Receipt_Insert");
        }
        public static bool UpdateReceipt(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID", obj.ID);
            Cls.AddParameter("@ReceiptCode", obj.ReceiptCode);
            Cls.AddParameter("@ReceiptDate", obj.ReceiptDate);
            Cls.AddParameter("@IDReceiptReason", obj.IDReceiptReason);
            Cls.AddParameter("@PayerRecipientCode", obj.PayerRecipientCode);
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@VoucherFile", obj.VoucherFile);
            Cls.AddParameter("@VoucherAmount", obj.VoucherAmount);
            Cls.AddParameter("@Amount", obj.Amount);
            Cls.AddParameter("@Amount_Remain", obj.Amount_Remain);
            Cls.AddParameter("@Note", obj.Note);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Receipt_Update", "@Msg");
            return true;
        }
        public static bool DeleteReceipt(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID", obj.ID);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Receipt_Delete", "@Msg");
            return true;
        }
        public static bool PlusReceipt(EtReceipt obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@VoucherAmount", obj.VoucherAmount);
            Cls.AddParameter("@AmountOutput", obj.Amount);
            obj.Result = Cls.ExecuteNonQueryOutput("sp_Receipt_Plus_Amount", "@Result");
            return true;
        }
        #endregion
    }

    public class EtReceipt
    {
        public int ID { get; set; }
        public string ReceiptCode { get; set; }
        public DateTime ReceiptDate { get; set; }
        public int IDReceiptReason { get; set; }
        public int PayerRecipientCode { get; set; }
        public int ? ID_Sell { get; set; }
        public string VoucherFile { get; set; }
        public float ? VoucherAmount { get; set; }
        public float ? Amount { get; set; }
        public string Note { get; set; }
        public int Msg { get; set; }
        public int Flag { get; set; }
        public float Result { get; set; }
        public float? Amount_Remain { get; set; }
    }
}
